#!/bin/bash
grep -q '/oracle/' /etc/passwd
DIRS="112_64 102_64"
RC=$?
if [ -f /home/k0528817/execute_move ]; then mv /home/k0528817/execute_move /home/k0528817/execute_move_${RANDOM}; fi
if [ $RC = 0 ]
then
        IDS=$(grep '/oracle/' /etc/passwd | cut -d: -f1 | xargs)
        for ID in $IDS
        do
                SID=$(grep "^${ID}:" /etc/passwd|cut -d: -f6 | cut -d/ -f3)
		for DIR in $DIRS
		do
			# files = initPR2.utl initPR2brarchive.utl initPR2brarchiveT.utl initPR2weekly.utl
			FILEDIR=/oracle/${SID}/${DIR}/dbs
                	FILES="$FILEDIR/init${SID}.utl $FILEDIR/init${SID}brarchive.utl $FILEDIR/init${SID}brarchiveT.utl $FILEDIR/init${SID}weekly.utl"
			for FILE in $FILES
			do	
	                	if [ -f $FILE ]
				then
					# change pool
					cat $FILE | sed 's/^[pP]ool =.*/pool = Daily/' > ${FILE}_md_edit2
					# change expiration ex.: expiration = 3 Weeks
					cat ${FILE}_md_edit2 | sed 's/^expiration =.*/expiration = 3 Weeks/' > ${FILE}_md_edit
					diff $FILE ${FILE}_md_edit 2>&1 > /dev/null
					RC=$?
					if [ $RC = 1 ]
					then
						FILEDIFF=${FILE}_md_edit_diff
						diff $FILE ${FILE}_md_edit > $FILEDIFF
						echo "cp ${FILE} ${FILE}_wrong_exp_2 ; mv ${FILE}_md_edit $FILE ; chown $ID ${FILE} ; chmod 644 ${FILE}" | tee /home/k0528817/execute_move
						echo -e "Original: $FILE\nNew: ${FILE}_md_edit" >> $FILEDIFF
						echo | tee -a $FILEDIFF
						echo "# Original: ${FILE}" | tee -a /home/k0528817/execute_move
						echo "# New: ${FILE}_md_edit" | tee -a /home/k0528817/execute_move
						echo "# Diff: $FILEDIFF" | tee -a /home/k0528817/execute_move
						echo "# Move: /home/k0528817/execute_move" | tee -a /home/k0528817/execute_move
						echo
						HOMEDIFF=$(echo ${FILE}_md_edit_diff | sed 's/\//-/g')
						echo "cp $FILEDIFF /home/k0528817/BKP$HOMEDIFF" | tee -a /home/k0528817/execute_move
					fi
		                fi
			done
		done
        done
fi
